<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Equipment Page</title>
</head>
<body>

<?php
//Rquire MySqlAdapter.
require_once(dirname(__FILE__).'/../db/MySqlAdapter.php');

/*
*check if "form time-slot" intersects "db time-slot".
*vars: times slots as strings.
*Return: true on intersaction., flase if there is no intersaction
*/
function isIntersects($db_start1, $db_end1, $form_start1, $form_end1){
		//echo "isIntersects \n".$db_start1."\t".$db_end1."\t".$form_start1."\t".$form_end1."\t";

		$db_start =	strtotime($db_start1); 
		$db_end =	strtotime($db_end1); 
		$form_start=strtotime($form_start1);
		$form_end =	strtotime($form_end1); 
		  
		//checks if form_slot intersects db_slot:		 
		if ( $db_start <= $form_start && $form_start < $db_end) {
			echo "The slots intersects with the following time-slot:<br>";
			echo "start slot: ".$db_start1;
			echo "<br/>end slot: ".$db_end1."<br/>";
			
			return true;		 
		} else if ( $db_start >= $form_start && $db_start < $form_end) {
					echo "The slots intersects with the following time-slot:<br>";
					echo "start slot: ".$db_start1;
					echo "<br/>end slot: ".$db_end1."<br/>";
					return true;
			} else
					//echo "The slots doesn't intersect.";
		return false;			
}

/*	connect to DB and create new if not exist
*	Step 1: open a connection to the DB
*/
$conn = MySqlAdapter::connect1();
if ($conn):
	$result = MySqlAdapter::connect2();
else:
	die ("Could not connect to db " . mysql_error());
endif;
?>

<center>
<br>

<?php
if (isset($_REQUEST['textFind']) && isset($_REQUEST['textID']) && isset($_REQUEST['startDate']) && isset($_REQUEST['endDate']))
{
	if ( $_REQUEST['textFind'] && $_REQUEST['textID'] && $_REQUEST['startDate'] && $_REQUEST['endDate'] )
	{
		if($result)
		{
			//Step 2: Select a database (schema)
			$result = MySqlAdapter::connect2()
			or die ("Could not select the schema heb_db: " . mysql_error());
		}		

		$eqid = "";
		$query = "SELECT `id` FROM `equipment` WHERE `name` = '".$_REQUEST['textFind']."';";
		$result = mysql_query($query) or die ("Invalid insert " . mysql_error());
		while ($row = mysql_fetch_array($result))
			$eqid = $row['id'];

		if($eqid == "")
			echo "Equipment was not found in the Data Base.check equipment name...";
		else
		{
			$query = "SELECT `lentDate`, `returnDate` FROM `reservation` WHERE `equipmentID` = '".$eqid."';";
			$result = mysql_query($query) or die ("Invalid insert " . mysql_error());
			
			$nointersaction= true;
			while (($row = mysql_fetch_array($result)) && $nointersaction)
			{			
				//echo $row['returnDate']." ||";
				$nointersaction = $nointersaction && !isIntersects($row['lentDate'], $row['returnDate'], $_REQUEST['startDate'], $_REQUEST['endDate']);
			}
			
			
			//if no intersaction was found, add new reservation relation to Data-Base.
			if($nointersaction){
				$query = "INSERT INTO `ohel_shelomo_2012`.`reservation` (`id`, `equipmentID`, `userID`, `lentDate`, `returnDate`) VALUES (NULL, '".$eqid."', '".$_REQUEST['textID']."', '".$_REQUEST['startDate']."', '".$_REQUEST['endDate']."')";
				mysql_query($query)   or die ("Invalid insert " . mysql_error());
				print("Thanks !<br>\n\tYour request for equipment was successfully sent.");
			}else
					print("Cann't add request: There is a previous request for that time-slot.\n Try another time-slot");			
		}		
	} else {
			echo "Please fill all the fileds";
		}
} else{ 
		echo "Please fill all the 3 fileds. Or enter this page in the acceptable way";
	}
?>
<br/>
<A HREF="javascript:history.back()">Back</A>
<br />
<img src="image/order.jpg" width="175" height="170" alt="orderEquipment" />
</center>

<?php
	//close connection with the Data Base
	mysql_close($conn);
?>
</body>
</html>
